﻿'copyright 2011 Jason Bean

Imports System.IO

Public Class Settings

    Private Sub btnHBBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHBBrowse.Click
        'set open file dialog properties
        OpenFileDialog1.Title = "Path to HandBrakeCLI.exe"
        OpenFileDialog1.Filter = "HandBrakeCLI.exe|HandBrakeCLI.exe"

        'set initial directory if path exists in textbox
        If txtHBPath.Text <> String.Empty Then
            OpenFileDialog1.InitialDirectory = Path.GetDirectoryName(txtHBPath.Text.ToString)
        End If

        'show dialog
        OpenFileDialog1.FilterIndex = 1
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            'set textbox if ok is pressed
            txtHBPath.Text = OpenFileDialog1.FileName.ToString
        End If
    End Sub

    Private Sub btnOutputBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutputBrowse.Click
        Dim browse As New FolderBrowserDialog

        'set browse dialog properties
        browse.Description = "Please select an output folder"
        browse.SelectedPath = txtOutputPath.Text.ToString

        'show dialog and if ok is pressed set textbox
        If browse.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtOutputPath.Text = browse.SelectedPath
        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        'set public removeads variable appropriately
        If chkRemoveAds.Checked Then
            RemoveAds = True
        Else
            RemoveAds = False
        End If

        'set public autocrop variable
        If chkAutoCrop.Checked Then
            AutoCrop = True
        Else
            AutoCrop = False
        End If

        If chkMetadata.Checked Then
            AutoTVMeta = True
        Else
            AutoTVMeta = False
        End If

        If chkMoviePoster.Checked Then
            AutoMoviePoster = True
        Else
            AutoMoviePoster = False
        End If

        If chkExportiTunes.Checked Then
            AutoExportiTunes = True
        Else
            AutoExportiTunes = False
        End If

        If chkDeleteOutfile.Checked Then
            autoDeleteExport = True
        Else
            autoDeleteExport = False
        End If

        'set registry values and public variables for each setting
        Application.UserAppDataRegistry.SetValue("HBPath", txtHBPath.Text.ToString)
        HBPath = txtHBPath.Text.ToString
        Application.UserAppDataRegistry.SetValue("mp4boxPath", txtMp4boxPath.Text.ToString)
        mp4boxPath = txtMp4boxPath.Text.ToString
        Application.UserAppDataRegistry.SetValue("APPath", txtAtomicParsleyPath.Text.ToString)
        APPath = txtAtomicParsleyPath.Text.ToString
        Application.UserAppDataRegistry.SetValue("OutPath", txtOutputPath.Text.ToString)
        OutPath = txtOutputPath.Text.ToString
        Application.UserAppDataRegistry.SetValue("RemoveAds", RemoveAds)
        Application.UserAppDataRegistry.SetValue("AutoCrop", AutoCrop)
        Application.UserAppDataRegistry.SetValue("AutoMeta", AutoTVMeta)
        Application.UserAppDataRegistry.SetValue("AutoMoviePoster", AutoMoviePoster)
        Application.UserAppDataRegistry.SetValue("AutoExportiTunes", AutoExportiTunes)
        Application.UserAppDataRegistry.SetValue("AutoDeleteExport", autoDeleteExport)

        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'close settings without saving anything
        Me.Close()
    End Sub

    Private Sub Settings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'load settings from public variables
        txtHBPath.Text = HBPath
        txtMp4boxPath.Text = mp4boxPath
        txtAtomicParsleyPath.Text = APPath
        txtOutputPath.Text = OutPath
        chkRemoveAds.Checked = RemoveAds
        chkAutoCrop.Checked = AutoCrop
        chkMetadata.Checked = AutoTVMeta
        chkMoviePoster.Checked = AutoMoviePoster
        chkExportiTunes.Checked = AutoExportiTunes
        If chkExportiTunes.Checked Then
            chkDeleteOutfile.Enabled = True
        Else
            chkDeleteOutfile.Enabled = False
        End If
        chkDeleteOutfile.Checked = autoDeleteExport
    End Sub

    Private Sub btnMp4boxBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMp4boxBrowse.Click
        'set open file dialog properties
        OpenFileDialog1.Title = "Path to MP4Box.exe"
        OpenFileDialog1.Filter = "MP4Box.exe|MP4Box.exe"

        'set initial directory if path exists in textbox
        If txtMp4boxPath.Text <> String.Empty Then
            OpenFileDialog1.InitialDirectory = Path.GetDirectoryName(txtMp4boxPath.Text.ToString)
        End If

        OpenFileDialog1.FilterIndex = 1
        'show dialog and if ok is pressed set textbox
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtMp4boxPath.Text = OpenFileDialog1.FileName.ToString
        End If
    End Sub

    Private Sub btnParsleyPath_Click(sender As System.Object, e As System.EventArgs) Handles btnParsleyPath.Click
        'set open file dialog properties
        OpenFileDialog1.Title = "Path to AtomicParsley.exe"
        OpenFileDialog1.Filter = "AtomicParsley.exe|AtomicParsley.exe"

        'set initial directory if path exists in textbox
        If txtAtomicParsleyPath.Text <> String.Empty Then
            OpenFileDialog1.InitialDirectory = Path.GetDirectoryName(txtAtomicParsleyPath.Text.ToString)
        End If

        OpenFileDialog1.FilterIndex = 1
        'show dialog and if ok is pressed set textbox
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtAtomicParsleyPath.Text = OpenFileDialog1.FileName.ToString
        End If
    End Sub

    Private Sub chkExportiTunes_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkExportiTunes.CheckedChanged
        If chkExportiTunes.Checked Then
            chkDeleteOutfile.Enabled = True
        Else
            chkDeleteOutfile.Enabled = False
        End If
    End Sub
End Class